<?php
namespace app\admin\controller;
use app\admin\controller\Common;
use app\admin\model\Conf as ConfModel;
class Conf extends Common
{
	public function lst()
    { 
        $conf=new ConfModel();
        if(request()->isPost()){
            $data=input('post.');

            foreach ($data as $k => $v) {
                $conf->update(['id'=>$k,'sort'=>$v]);
            }
            $this->success('更新成功！',url('conf/lst'));
            return;
        }

        $confres=$conf->order('sort asc')->paginate(5);
        //dump($confres);die;
        $this->assign('confres',$confres);
        return view('list');
    }

    public function add()
    {
        if(request()->isPost()){
             $data=input('post.');
             //验证规则
             $validate = \think\Loader::validate('Conf');//实例化link验证器
            if(!$validate->scene('add')->check($data)){
                $this->error($validate->getError());
            }
             if($data['values']){
                $data['values']=str_replace('，', ',',$data['values']);
             }
             $insert=ConfModel::create($data);
            if($insert){
                $this->success('添加配置項成功！',url('conf/lst'));
            }else{
                $this->error('添加配置項失败！');
            }
            return;
        }
        return view();
    }

    public function edit()
    {
        $conf=new ConfModel();
        if(request()->isPost()){
            $data=input('post.');
            //验证规则
            $validate = \think\Loader::validate('Conf');//实例化link验证器
            if(!$validate->scene('edit')->check($data)){
                $this->error($validate->getError());
            }
            if($data['values']){
                $data['values']=str_replace('，', ',',$data['values']);
             }
            $conf->save($data,['id'=>$data['id']]);
            if($conf){
                $this->success('修改配置項成功！',url('conf/lst'));
            }else{
                $this->error('修改配置項失败！');
            }
        }

        $confs=$conf->find(input('id'));
        $this->assign('confs',$confs);
        return view();
    }

    public function delete()
    {
        $del=db('conf')->delete(input('id'));
        if($del){
             $this->success('删除配置项成功！',url('conf/lst'));
            }else{
                $this->error('删除配置项失败！');
            }
        return view();
    }
    //取出所有配置项
    public function conf()
    {
        if(request()->isPost()){
            $data=input('post.');
            //dump($data);die;
            $_datafrom=array();
            foreach ($data as $k => $v) {
                $_datafrom[]=$k;
            }
            
            $_confres=db('conf')->field('enname')->select();
            //得到数据库的一维数组
            $confres=array();
            foreach ($_confres as $k => $v) {
                $confres[]=$v['enname'];
            }
            //得到勾选掉的数组
            $noexsit=array();
            foreach ($confres as $k => $v) {
                if(!in_array($v, $_datafrom)){
                    $noexsit[]=$v;
                }
            }
            //dump($noexsit);die;
           //清除复选框里的内容
           if( $noexsit !=''){
            foreach ($noexsit as $k => $v) {
                 ConfModel::where('enname',$v)->update(['value'=>'']);
               }
           }
            
           //修改配置项内容
            if($data){
                foreach ($data as $k => $v) {
                ConfModel::where('enname',$k)->update(['value'=>$v]);
                } 
            }
            
            $this->success('配置项修改成功！',url('conf/conf'));
            return;
        }
        $confres=ConfModel::order('sort desc')->select();
        //dump($confres);die;
        $this->assign('confres',$confres);
        return view('conflist');
    }
}
